package com.example.recursion;

import java.util.ArrayList;
import java.util.List;

/**
 * @Date 2022/7/9
 * @Created by Jonathan
 */
public class ConvertToLetterStr {
    public static void main(String[] args) {
        //
        //final List<String> list = getResult("111");
        // System.out.println(list);
        System.out.println(getNum("11111"));
    }

    public static List<String> getResult(String str) {
        List<String> ans = new ArrayList<>();
        process(0, str, ans);
        return ans;
    }


    public static int getNum(String str) {
        List<String> ret = new ArrayList<>();
        final int num = process2(str.toCharArray(), 0, "", ret);
        System.out.println(ret);
        return num;
    }


    /**
     * 没看视频之前自己写的版本
     */
    public static void process(int i, String str, List<String> ans) {
        if (i == str.length()) {
            return;
        }
        for (int j = i; j < str.length(); j++) {
            final String substring = str.substring(i, j + 1);
            final int num = Integer.parseInt(substring);
            if (num >= 1 && num <= 9) {
                char ret = (char) ('A' + (num - 1));
                ans.add(String.valueOf(ret));
                process(i + 1, str, ans);
            } else if (num >= 10 && num <= 26) {
                char ret = (char) ('A' + (num - 1));
                ans.add(String.valueOf(ret));
                process(i + 2, str, ans);
            } else {
                return;
            }
            process(i + 1, str, ans);
            //System.out.println(substring);
        }
    }


    /**
     * 思考： 怎么才能把这个转化结果记录下来呢？
     * ans表示已经转换了的部分
     */
    public static int process2(char[] str, int i, String ans, List<String> ret) {
        if (i == str.length) {
            ret.add(ans);
            return 1;
        }
        if (str[i] == '0') {
            return 0;
        }

        if (str[i] == '1') {
            // 将i取出来
            final String join = join(ans, str[i]);
            int res = process2(str, i + 1, join, ret);// i自己作为单独的部分，后续有多少种方法
            if (i + 1 < str.length) {
                final String join1 = join(ans, str[i], str[i + 1]);
                res += process2(str, i + 2, join1, ret);// (i和i+1)作为单独的部分，后续有多少种方法
            }
            return res;
        }
        if (str[i] == '2') {
            final String join = join(ans, str[i]);
            int res = process2(str, i + 1, join, ret); // i自己作为单独的部分，后续有多少种方法
            if (i + 1 < str.length && (str[i + 1] > '0' && str[i + 1] <= '6')) {
                final String join1 = join(ans, str[i], str[i + 1]);
                res += process2(str, i + 2, join1, ret);// (i和i+1)作为单独的部分，后续有多少种方法
            }
            return res;
        }
        // str[i] == '3' ~ '9'
        final String join = join(ans, str[i]);
        return process2(str, i + 1, join, ret);
    }

    /**
     * c是'1'
     */
    private static String join(String original, char c) {
        char ret = (char) ('A' + (Integer.parseInt(String.valueOf(c)) - 1));
        return original + ret;
    }

    private static String join(String original, char c1, char c2) {
        char[] chars = new char[2];
        chars[0] = c1;
        chars[1] = c2;
        char ret = (char) ('A' + (Integer.parseInt(String.valueOf(chars)) - 1));
        return original + ret;
    }
}
